package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;
    
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public AjaxResult getCancelledReservationCount() {
        // 获取当前登录用户ID
        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        
        // 查询当天取消预约数量
        int count = reservationService.getCancelledReservationCount(userId);
        
        // 返回结果
        return success(count);
    }
    
    @GetMapping("/countByTime")
    @ApiOperation("查询指定日期预约时间段剩余次数")
    public AjaxResult countByTime(@RequestParam(value = "time", required = true) Long time) {
        // 查询指定日期预约时间段剩余次数
        List<TimeCountVo> countList = reservationService.countByTime(time);
        
        // 返回结果
        return success(countList);
    }
    
    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult addReservation(@Valid @RequestBody ReservationDto reservationDto) {
        // 新增预约
        boolean result = reservationService.addReservation(reservationDto);
        
        // 返回结果
        return result ? success() : error("预约失败");
    }
    
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public AjaxResult pageQuery(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status) {
        // 分页查询预约
        TableDataInfo tableDataInfo = reservationService.pageQuery(pageNum, pageSize, status);
        
        // 返回结果
        return success(tableDataInfo);
    }
    
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancelReservation(@PathVariable("id") Long id) {
        try {
            // 取消预约
            boolean result = reservationService.cancelReservation(id);
            
            // 返回结果
            return result ? success() : error("取消预约失败");
        } catch (RuntimeException e) {
            return error(e.getMessage());
        }
    }
}